package com.baizhi.service.impl;

import com.baizhi.dao.CommentDao;
import com.baizhi.entity.Comment;
import com.baizhi.service.CommentService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CommentServiceImpl implements CommentService {
    @Override
    public void addComment(Comment c) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            CommentDao dao = sqlSession.getMapper(CommentDao.class);
            dao.insertComment(c);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void updateComment(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            CommentDao dao = sqlSession.getMapper(CommentDao.class);
            dao.updateComment(id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public Comment selectOne(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        CommentDao dao = sqlSession.getMapper(CommentDao.class);
        Comment comment = dao.selectOne(id);
        MyBatisUtils.close(sqlSession);
        return comment;
    }

    @Override
    public List<Comment> selectAll() {
        SqlSession sqlSession = MyBatisUtils.openSession();
        CommentDao dao = sqlSession.getMapper(CommentDao.class);
        List<Comment> list = dao.selectAll();
        MyBatisUtils.close(sqlSession);
        return list;
    }
}
